/*
 * Copyright (c) 2006, 2023, webrx.cn All rights reserved.
 *
 */

package cn.webrx;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;

/**
 * <p>Project: jse2303 - FileUtil
 * <p>Powered by webrx On 2023-07-20 17:29:46
 * <p>描述：<p>
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class FileUtil {
    public static void append(File src, File dst) {
        try (BufferedReader br = new BufferedReader(new FileReader(src));
             BufferedWriter wr = new BufferedWriter(new FileWriter(dst, true));
        ) {
            String header = """
                    ----------------------------------------------------------------------------
                    文 件名：%s
                    位置：%s
                    行数：%d
                    建立日期：%tF %<tT
                    文件内容：
                    ----------------------------------------------------------------------------
                    """;
            String h = String.format(header, src.getName(), src.getAbsolutePath(), getFileRow(src.getAbsolutePath()), src.lastModified());

            wr.write(h);
            while (br.ready()) {
                wr.write(br.readLine() + "\r\n");
            }

            wr.write("----------------------------------------------------------------------------\r\n\r\n\r\n");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static long getFileRow(String file) {
        long rows = 0;
        //字符流
        try (FileReader reader = new FileReader(file)) {
            StringBuilder sbu = new StringBuilder();
            while (reader.ready()) {
                sbu.append(String.format("%c", reader.read()));
            }
            rows = sbu.toString().lines().count();
        } catch (Exception e) {

        }
        return rows;
    }

    public static void copyFile(File src, File dst) {
        try (var is = new FileInputStream(src); var out = new FileOutputStream(dst)) {
            is.transferTo(out);
        } catch (Exception e) {
        }
    }

    public static void copyFile(String src, String dst) {
        copyFile(new File(src), new File(dst));
    }


    /**
     * 实现目录复制
     *
     * @param src
     * @param dst
     */
    public static void copyTree(File src, File dst) {
        if (!dst.exists()) dst.mkdirs();
        File[] files = src.listFiles();
        for (File f : files) {
            if (f.isDirectory()) {
                copyTree(f, new File(dst, f.getName()));
            } else {
                copyFile(f, new File(dst, f.getName()));
            }
        }
    }


    /**
     * Description: 递归删除目录，或删除文件
     *
     * @date: 2023/7/21 9:22
     * @params: [java.io.File]
     * @return: void
     */
    public static void delTree(File src) {
        if (src.isDirectory()) {
            File[] fs = src.listFiles();
            for (File f : fs) {
                if (f.isDirectory()) {
                    delTree(f);
                } else {
                    f.delete();
                }
            }
        }
        src.delete();
    }
}
